﻿using System;
using SpotOffer.AppConstants;
using SpotOffer.CustomViews;
using SpotOffer.DataModels;
using SpotOffer.Resource;
using SpotOffer.ViewModels;
using Xamarin.Forms;
using XLabs.Forms.Controls;
using XLabs.Forms.Mvvm;

namespace SpotOffer.View
{
    public class MenuPage : BasePage
    {
        public Button ButtonCity, ButtonShare, ButtonSettings;
        public ListView ListViewMenu { get; set; }
        public MenuPage()
        {
            Icon = "menu.png";
            Title = AppResources.FirstChooseCity;
            
            ButtonSettings = new AppViews.ButtonSpotOffer
            {
                Text = AppResources.Settings,
            };

            ButtonCity = new AppViews.ButtonSpotOffer
            {
                Text = AppResources.ChooseCity,
            };


            MakeMenu();
            Content = MakeLayout();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            SetBinding(TitleProperty, new Binding("Title"));
        }

        private Xamarin.Forms.View MakeLayout()
        {
            var stackLayout = AppViews.ContentStackLayout();

            if (Device.OS == TargetPlatform.iOS)
            {
                var label = new ContentView
                {
                    Padding = new Thickness(3, 38, 0, 3),
                    Content = new Label
                    {
                        TextColor = AppColors.Grey,
                        //  Text = AppResources.ChooseCity,
                    }
                };
                stackLayout.Children.Add(label);
            }
            stackLayout.Children.Add(ListViewMenu);
            stackLayout.Children.Add(ButtonCity);
            stackLayout.Children.Add(ButtonSettings);
            stackLayout.BackgroundColor = AppColors.White;
            //stackLayout.Children.Add(ButtonShare);

            return stackLayout;
        }

        private void MakeMenu()
        {
            ListViewMenu = AppViews.ReturnListView();
            ListViewMenu.BackgroundColor = AppColors.White;
            ListViewMenu.SetBinding(ListView.ItemsSourceProperty, new Binding("CategoryList"));
            ListViewMenu.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedCategory", BindingMode.TwoWay));
            ListViewMenu.ItemTemplate = new DataTemplate(() =>
            {
                var labelCategory = AppViews.ItemLabel();
                labelCategory.HorizontalOptions = LayoutOptions.Start;
                labelCategory.SetBinding(Label.TextProperty, "categoryName");
                labelCategory.SetBinding(Label.TextColorProperty, "ItemTextColor");
                labelCategory.SetBinding(BackgroundColorProperty, "ItemBackgroundColor");

                var finalview = AppViews.ItemContentView(labelCategory);
                finalview.SetBinding(BackgroundColorProperty, "ItemBackgroundColor");

                return new ViewCell
                {
                    View = finalview
                };
            });
        }


    }
}
